

import 'package:flutter/material.dart';
import 'package:xim/widget/dash_chat_2/dash_chat_2.dart';
import 'package:xim/widget/dash_chat_2/src/widgets/image_provider/image_provider_mobile.dart';

/// 自定义聊天对话框展示的用户头像
class ChatAvatar extends StatelessWidget{

  /// The URL of the user's profile picture
  final ChatUser user;

  /// Size of the avatar
  final double size;

  /// Placeholder image in case there is no initials ot he profile image do not load
  final ImageProvider? fallbackImage;

  /// Function to call when the user long press on the avatar
  final Function? onLongPressAvatar;

  /// Function to call when the user press on the avatar
  final Function? onPressAvatar;

  const ChatAvatar({
    super.key,
    required this.user,
    this.size = 42,
    this.fallbackImage,
    this.onPressAvatar,
    this.onLongPressAvatar,
  });

  /// Get the initials of the user
  String getInitials() {
    return (user.firstName == null || user.firstName!.isEmpty
        ? ''
        : user.firstName![0]) +
        (user.lastName == null || user.lastName!.isEmpty
            ? ''
            : user.lastName![0]);
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPressAvatar != null ? () => onPressAvatar!(user) : null,
      onLongPress:
      onLongPressAvatar != null ? () => onLongPressAvatar!(user) : null,
      child: Padding(
        padding: const EdgeInsets.symmetric(
          horizontal: 10,
        ),
        child: SizedBox(
          height: size,
          width: size,
          child: Stack(
            alignment: Alignment.center,
            children: <Widget>[
              ClipOval(
                child: Container(
                  color: Colors.grey[200],
                  child: getInitials().isNotEmpty
                      ? Center(
                    child: Text(
                      getInitials(),
                      style: TextStyle(
                        color: Colors.black,
                        fontSize: size * 0.35,
                      ),
                    ),
                  )
                      : Image(
                    image: fallbackImage ??
                        const AssetImage(
                          'assets/profile_placeholder.png',
                          package: 'dash_chat_2',
                        ),
                  ),
                ),
              ),
              if (user.profileImage != null && user.profileImage!.isNotEmpty)
                Center(
                  child: ClipRRect(
                    borderRadius: const BorderRadius.all(Radius.circular(6)),
                    child: FadeInImage(
                      width: size,
                      height: size,
                      fit: BoxFit.cover,
                      image: getImageProvider(user.profileImage!),
                      placeholder: fallbackImage ??
                          const AssetImage(
                            'assets/profile_placeholder.png',
                            package: 'dash_chat_2',
                          ),
                    ),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }
}